package com.example.baselibrary.db.dao;

import android.text.TextUtils;

import com.example.baselibrary.db.BTDBHelper;
import com.example.baselibrary.db.table.GroupMemberTable;

import java.sql.SQLException;
import java.util.List;

import cc.vv.lklibrary.db.dao.DBBaseTableDao;
import cc.vv.lklibrary.db.help.DBBaseOpenHelper;

/**
 * 群组成员管理
 *
 * @author: NPF
 * @date: 2018/4/10.
 */
public class GroupMemberDao extends DBBaseTableDao<GroupMemberTable, String> {
    public GroupMemberDao(DBBaseOpenHelper openHelper) {
        super(openHelper);
    }

    private static GroupMemberDao mInstance;

    /**
     * 单例模式获取实例
     */
    public static GroupMemberDao getInstance() {
        if (null == mInstance) {
            synchronized (GroupMemberDao.class) {
                if (null == mInstance) {
                    mInstance = new GroupMemberDao(BTDBHelper.getInstance());
                }
            }
        }
        return mInstance;
    }

    /**
     * 批量插入,不要指定ID
     *
     * @param groupId
     * @param groupMemberTableList
     * @return
     */
    public int insert(String groupId, List<GroupMemberTable> groupMemberTableList) {
        int result = -1;
        if (!TextUtils.isEmpty(groupId) && null != groupMemberTableList && !groupMemberTableList.isEmpty()) {
            try {
                deleteMembers(groupId);
                result = this.mDao.create(groupMemberTableList);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    /**
     * 删除指定群组中的成员
     *
     * @param groupId
     * @return
     */
    private int deleteMembers(String groupId) throws SQLException {
        String sql = "DELETE FROM groupMember WHERE groupId = ?";
        int result = this.mDao.executeRaw(sql, groupId);
        return result;
    }


}
